package cn.zlp.hrm.service.impl;

import cn.zlp.hrm.config.RabbitMQConfig;
import cn.zlp.hrm.domain.Order;
import cn.zlp.hrm.domain.OrderItem;
import cn.zlp.hrm.exception.ValidUtils;
import cn.zlp.hrm.mapper.OrderItemMapper;
import cn.zlp.hrm.mapper.OrderMapper;
import cn.zlp.hrm.service.IOrderService;
import cn.zlp.hrm.util.AjaxResult;
import cn.zlp.hrm.util.CodeGenerateUtils;
import cn.zlp.hrm.vo.OrderCancelVo2Queue;
import cn.zlp.hrm.vo.PreCreateOrderData;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zlp
 * @since 2022-01-23
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    @Transactional
    public AjaxResult killOrder(Map<String, String> params) {
        //1 校验参数
        //1.1 合法向校验
        ValidUtils.assertNotNull(params,"非法请求！");
        String preCreateOrderSn = params.get("preCreateOrderSn");
        String payMethod = params.get("payMethod"); //4,5步使用的
        ValidUtils.assertNotNull(preCreateOrderSn,"非法请求！");
        ValidUtils.assertNotNull(payMethod,"非法请求！");
        //1.2 判断预创订单是否过期
//        String hashKey = "preCreateOrder"+userId;
//        String hashKkey ="_"+killCourse.getSessionNumber()+killCourseId;
        String hashK= preCreateOrderSn.split("_")[0];
        String hashKk= preCreateOrderSn.split("_")[1];
        BoundHashOperations<String,String, PreCreateOrderData> operations =
                redisTemplate.boundHashOps(hashK);
        PreCreateOrderData preCreateOrderData = operations.get(hashKk);
        ValidUtils.assertNotNull(preCreateOrderData,"请确认秒杀成功后，再操作！");
        //2 创建订单
        //2.1 订单
        Order order = new Order();
        order.setCount(1);
        order.setCreateTime(new Date());
        order.setLastPayTime(DateUtils.addMinutes(new Date(),1));//一分钟过期
        order.setOrderNo(CodeGenerateUtils.generateOrderSn(preCreateOrderData.getUserId()));
//        order.setSessionsId(preCreateOrderData.getSessionNumber());
        order.setUserId(preCreateOrderData.getUserId());
        order.setTotalPrice(preCreateOrderData.getPrice());
        order.setStatusOrder(0);
        order.setStatusPay(0+"");
        order.setOrderType(0);
        super.insert(order);
        //2.2 订单明细-对秒杀而言一个订单就一个明细
        OrderItem item = new OrderItem();
        item.setOrderId(order.getId());
        item.setCount(1);
        item.setCreateTime(new Date());
        item.setPrice(preCreateOrderData.getPrice());
        item.setSessionNumber(preCreateOrderData.getSessionNumber());
        orderItemMapper.insert(item);
        //3 删除预创订单
        redisTemplate.opsForHash().delete(hashK,hashKk);
        //4 创建支付单 @TODO
        //5 发起支付请求 @TODO
        //6 订单倒计时取消
        OrderCancelVo2Queue vo = new OrderCancelVo2Queue();
        vo.setOrderId(order.getId());
        vo.setKillCourseId(preCreateOrderData.getKillCousreId());
        rabbitTemplate.convertAndSend(RabbitMQConfig.ORDER_EXCHANGE,
                RabbitMQConfig.ROUTINGKEY_QUEUE_DEAD,vo);
        return AjaxResult.me().setResultObj("支付包的数据！");
    }
}
